package bupt.party.online.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;

import bupt.party.online.model.Student;
import bupt.party.online.util.SessionConstants;
import bupt.party.online.video.Video;
import bupt.party.online.video.VideoMenu;
import bupt.party.online.video.VideoMgr;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ognl.OgnlValueStack;

/**
 * Video Action for BuptPartyOnlineLine website
 * 
 * @author lennydou
 * @version 1.0
 */
public class VideoAction extends ActionSupport{
	/**
	 * The default method
	 */
	
	public String execute(){
		if(!this.checkForAuth(SessionConstants.STU_LEVEL) ) return "loginUI";
		return SUCCESS;
	}
	
	public String view(){
		if(!this.checkForAuth(SessionConstants.STU_LEVEL) ) return "loginUI";
		
		// Get the menu list
		String menuStr = VideoMenu.getInstance().getMenuStr();
		HttpServletRequest request = ServletActionContext.getRequest(); 
		request.setAttribute("menuStr", menuStr);
		
		return "view";
	}
	
	public String details(){
		if(!this.checkForAuth(SessionConstants.STU_LEVEL) ) return "loginUI";
		return "details";
	}
	
	public String playVideo(){
		if(!this.checkForAuth(SessionConstants.STU_LEVEL) ) return "loginUI";
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String videopath = request.getParameter("videopath");
		String categoryId = request.getParameter("categoryId");
		
		Video target = null;
		List<Video> videoList = VideoMgr.getInstance().getVideos(categoryId, Video.STATUS_PUBLISHED);
		for(int i = 0; i < videoList.size(); i++){
			Video temp = videoList.get(i);
			if(temp.getVideopath().trim().equals(videopath.trim())){
				target = temp;
			}
		}
		request.setAttribute("videoFullPath", target.getVideopath());
		request.setAttribute("videoName", target.getName());
		return "playVideo";
	}
	
	
	public boolean checkForAuth(int requiredLevel){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		Object user = session.getAttribute(SessionConstants.SESSION_USER); 
		
		if(!(user instanceof Student)) return false;
		
		Student student	 = (Student) user;
		
		return student.getAdminLevel() == requiredLevel;
	}

	public void queryState(){
		String resultStr = null;
		HttpServletRequest request = ServletActionContext.getRequest(); 
		HttpSession session = request.getSession();
		Object userObj = session.getAttribute(SessionConstants.SESSION_USER);
		if( userObj == null ){
			resultStr = "0";
		}else{
			resultStr = "1";
		}
		
		// Write back
		OgnlValueStack stack = (OgnlValueStack) request.getAttribute("struts.valueStack");
		HttpServletResponse response = (HttpServletResponse) stack.getContext().get(StrutsStatics.HTTP_RESPONSE);
		try {
			response.setHeader("Content-Type", "text/plain; charset=UTF-8");
			response.getWriter().write(resultStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
